
import 'package:fate_blind_box/utils/common_widget.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
import '../../../network/Api.dart';
import '../../../theme/theme_color.dart';
import '../../../utils/loading.dart';
import '../../../utils/page.dart';
import '../../../utils/provider.dart';
import '../../../utils/snack_bar.dart';
import 'logoutFinish.dart';

class logoutNextUI extends StatelessWidget {
  const logoutNextUI({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const _logoutNextUIWidget();
  }
}

class _logoutNextUIWidget extends StatefulWidget {
  const _logoutNextUIWidget({Key? key}) : super(key: key);

  @override
  State<_logoutNextUIWidget> createState() => _logoutNextUIWidgetState();
}

class _logoutNextUIWidgetState extends State<_logoutNextUIWidget> {
  @override
  void dispose() {

    super.dispose();
  }


  final List _listData = [
    {
      'title': '账号资产已结清',
      'subTitle':'剩余积分少于100',

    },
    {
      'title': '账号未封禁',
      'subTitle': '账号当前处于非封禁状态',
    },
    {
      'title': '账号无安全风险',
      'subTitle': '账号不可处于被检查/限制状态',
      'subTitleSec': '30天内不可有异常登陆/修改密码登陆',
      'subTitleThird': '账号下不可有未处理完的投诉/举报',
    },
  ];

  @override
  void initState() {}

  @override
  Widget build(BuildContext context) {

    return AppWidget.navTitleBar(
      title:'注销账号',
        childWidget: SingleChildScrollView(
          child: Column(

            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                margin: EdgeInsets.only(left: 10,top: 10,bottom: 10),
                height: 30,
                alignment: Alignment.topLeft,
                child: Text('需满足一下条件，才能注销当前账号'),
              ),
            _buildActionButton(
               _listData[0]['title'],_listData[0]['subTitle'],90.0),
               const SizedBox(height: 10,),
              _buildActionButton(
                _listData[1]['title'],_listData[1]['subTitle'],90.0),
              const SizedBox(height: 10,),
              _buildActionButton(
                  _listData[2]['title'],_listData[2]['subTitle'],140.0),
              GestureDetector(
                child: Container(
                    margin: const EdgeInsets.fromLTRB(59, 30, 59, 0),
                    height: 45,
                    decoration: ShapeDecoration(
                      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(22.5)),
                      color: const Color(0xFF7F69D9),
                    ),
                    width: double.maxFinite,
                    child: const Center(
                      child: Text(
                        "下一步",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    )
                ),
                onTap: () {

                  _meMemberLogout();
                  Navigator.of(context).push(MaterialPageRoute(builder: (ctx) => const logoutFinishUI()));
                },
              ),
            ],
          ),
        ));
  }

  _meMemberLogout() async {
    final res = await Api.getInstance().memberLogout(Provider.memberId);
    final data = res['data'];

    if (res['code'] == 0 && res['data'] != null) {
      _signOut();//退出登录
      ThemeSnackBar.show(context, '注销成功');
    } else {
      ThemeSnackBar.show(context, '注销失败');
    }
  }
  //添加退出
  void _signOut() async {
    try {
      await EMClient.getInstance.logout(true);
      toast('退出成功~');
      Provider.remove('token');
      Navigate.toLogin();
    } on EMError catch (e) {
      toast('退出失败~');
    }
  }
  Widget _buildActionButton(String title,String subTitle,double Height) {
    return GestureDetector(
        onTap: () {
          // _onClick(index);
        },
        child: Container(
            decoration: ShapeDecoration(
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5)),
              color: Colors.white,
            ),
          height: Height,
          padding: const EdgeInsets.only(left:10,right: 10,bottom: 10),
          margin: const EdgeInsets.symmetric(horizontal: 12),
          child:Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const SizedBox(height: 10,),
              Text(
                  title,
                  style: TextStyle(color: ThemeColor.textColor, fontSize: 16,fontWeight: FontWeight.bold),
              ),
              const SizedBox(height: 10,),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [

                  Height == 90.0
                      ?
                  (_nButton(subTitle))
                      :
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [

                     _nButton(subTitle),
                    _nButton(_listData[2]['subTitleSec']),
                    _nButton(_listData[2]['subTitleThird']),
                  ],
              )
                ],
              ),
            ],
          )
        )
    );
  }

  Widget _nButton(String subTitle) {

    return Row(
      mainAxisAlignment: MainAxisAlignment.start,

      children: [
        Image.asset(
          'images/ic_greenYes.png',
          width: 16,
          height: 16,
        ),
        const SizedBox(
          width: 5,
        ),
        Text(
          subTitle,
          style: TextStyle(color: ThemeColor.textColor, fontSize: 15),
        ),
      ],
    );

  }
}
